package com.kaifamiao.wendao.dao;

import com.kaifamiao.utils.JdbcHelper;
import com.kaifamiao.utils.RowHandler;
import com.kaifamiao.wendao.entity.Comment;
import com.kaifamiao.wendao.entity.Customer;
import com.kaifamiao.wendao.entity.Topic;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;

public class CommentDao implements Dao<Comment,Integer> {

    private RowHandler<Comment> rowHandler = new RowHandler<Comment>() {
        @Override
        public Comment handle(ResultSet rs) throws SQLException {
            Comment c = new Comment();
            c.setId( rs.getInt( "id" ) );
            c.setContent( rs.getString( "content" ) );
            c.setPublishIp( rs.getString( "publish_ip" ) );

            Timestamp ts = rs.getTimestamp( "publish_time" );
            if(  ts != null ) {
                c.setPublishTime( ts.toLocalDateTime() );
            }

            // 获得发布者的编号
            Integer cid = rs.getInt( "cid" );
            if( cid != null ) {
                Customer p = new Customer();
                p.setId(cid);
                c.setPublisher(p);
            }

            return c;
        }
    };

    public List<Comment> findByTopicId( Integer topicId ){
        JdbcHelper h = JdbcHelper.getInstance();
        String sql = "SELECT id , content , publish_ip , publish_time , cid " +
                     "FROM t_comments " +
                     "WHERE tid = ? " +
                     "ORDER BY publish_time DESC " ;
        List<Comment> comments = h.query( sql , rowHandler , topicId );
        h.release();
        return comments ;
    }

    @Override
    public Comment find(Integer id) {
        return null;
    }

    @Override
    public List<Comment> findAll() {
        return null;
    }

    /**
     * 查询数据库返回新的数据库主键值
     * @return
     */
    public Integer nextId(){
        JdbcHelper helper = JdbcHelper.getInstance();
        // MySQL
        String sql = "SELECT IFNULL( max( id ) , 0 ) + 1 FROM t_comments" ;
        // Oracle
        // String sql = "SELECT NVL( max( id ) , 0 ) + 1 FROM t_comments" ;
        Integer id = helper.single(sql, new RowHandler<Integer>() {
            @Override
            public Integer handle(ResultSet rs) throws SQLException {
                return rs.getInt( 1 );
            }
        });
        helper.release();
        return id ;
    }

    @Override
    public boolean save(Comment c) {
        JdbcHelper h = JdbcHelper.getInstance();
        String sql = "INSERT INTO t_comments " +
                     "( id , content , publish_ip , publish_time , cid , tid ) " +
                     "VALUES ( ? , ? , ? , ? , ? , ? ) ";
        LocalDateTime publishTime = c.getPublishTime();
        Timestamp t = Timestamp.valueOf( publishTime );
        Integer cid = c.getPublisher().getId() ;
        Integer tid = c.getTopic().getId();
        Object[] params = { c.getId() , c.getContent() , c.getPublishIp() , t , cid , tid };
        int count = h.execute( sql , params );
        h.release();
        return count == 1 ;
    }

    @Override
    public boolean update(Comment comment) {
        return false;
    }

    @Override
    public boolean remove(Integer id) {
        return false;
    }

}
